package org.everyday;

import java.util.TreeSet;

/**
 * @version v1.0.0
 * 创建时间：2022/7/5 09:03
 */
public class L729 {

}
class MyCalendar {
    TreeSet<int[]> booked;

    public MyCalendar() {
        booked = new TreeSet<int[]>((a, b) -> a[0] - b[0]);//升序
    }

    public boolean book(int start, int end) {
        // 如果为空直接加入
        if (booked.isEmpty()) {
            booked.add(new int[]{start, end});
            return true;
        }
        // 如果不为空,需要判断前后是否有交叉
        int[] tmp = {end, 0};
        int[] after = booked.ceiling(tmp);
        int[] pre = booked.lower(tmp);
        if (after == booked.first() || pre[1] <= start) {
            booked.add(new int[]{start, end});
            return true;
        }
        return false;
    }
}